package view.toolboxComponents;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import controller.CustomEvent;
import controller.EventHandler;
import controller.ToolBoxEventHandler;
/** * A class to encapsulate the name panel for an object on the designer * @author Daniel Bond, Mohammed Abdullah, Gideon Pyzer * */
@SuppressWarnings("serial")
public class PrescriptionObjectNamePanel extends AbstractPanel{
	private JLabel lblObjName;
	private JTextField txtObjName;
	private EventHandler eventHandler;
	public PrescriptionObjectNamePanel(){
		super();		
		txtObjName = new JTextField();
		txtObjName.setMaximumSize(new Dimension(120, 20));
		lblObjName = new JLabel("Name");
		buildPanel();		
		txtObjName.addKeyListener(new KeyListener(){
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					CustomEvent event = new CustomEvent("presObjectNameChange", this, txtObjName.getText());
					PrescriptionObjectNamePanel.this.eventHandler = new ToolBoxEventHandler();
					eventHandler.createEvent(event);
				}
			}
			@Override
			public void keyReleased(KeyEvent e) {
			}
			@Override
			public void keyTyped(KeyEvent e) {			
			}
		});
	}
	/**
	 * Sets the text in the name panel to that of the parameter
	 * @param text the text to set to the name field
	 */
	public void setText(String text){
		txtObjName.setText(text);
	}
	/**
	 * Build prescription name panel
	 */
	private void buildPanel() {
		this.add(lblObjName);
		makeRigidArea();
		this.add(txtObjName);
	}	@Override
	public void resetComponent() {
		txtObjName.setText("");
	}
}